using Digitalmes.Domain.Entities.Mes.Productions;

namespace Digitalmes.Application.Mes.Productions.Consumptions.Queries;

/// <summary>
/// 分页查询产品用料命令。
/// </summary>
/// <param name="Sn">产品零件码</param>
/// <param name="Barcode">物料条码</param>
public sealed record GetMaterialConsumptionListWithPagedQuery(
    string? Sn,
    string? Barcode,
    int PageNumber,
    int PageSize) : IPagedQuery, IRequest<IResult<PagedList<ProdMaterialConsumptionDto>>>;

internal sealed class GetMaterialConsumptionListWithPagedQueryHandler(IRepository<ProdMaterialConsumption> repo)
    : IRequestHandler<GetMaterialConsumptionListWithPagedQuery, IResult<PagedList<ProdMaterialConsumptionDto>>>
{
    public async Task<IResult<PagedList<ProdMaterialConsumptionDto>>> Handle(GetMaterialConsumptionListWithPagedQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(request.Sn), m => m.Sn == request.Sn)
            .WhereIF(!string.IsNullOrEmpty(request.Barcode), m => m.Barcode == request.Barcode)
            .GetPagedListAsync(request.PageNumber, request.PageSize, cancellationToken);
        return Result.From(items.Adapt<PagedList<ProdMaterialConsumptionDto>>());
    }
}
